package com.zhuhjay.common.core.socketio.listener;

import cn.hutool.core.util.StrUtil;
import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.HandshakeData;
import com.zhuhjay.common.redis.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static com.zhuhjay.common.base.constant.CacheConstant.TOKEN_PREFIX;

/**
 * @author ZhuHJay
 * @date 2023/7/10 10:07
 */
@Slf4j
@Component
public class LoginAuthorizationListener implements AuthorizationListener {
    @Override
    public boolean isAuthorized(HandshakeData data) {
        log.info("进行用户身份校验 LoginAuthorizationListener");
        String token = data.getSingleUrlParam("token");
        if (StrUtil.isNotBlank(token)) {
            String uid = RedisUtils.getCache(TOKEN_PREFIX + token);
            return StrUtil.isNotBlank(uid);
        }
        return false;
    }
}
